import subprocess
import IPy
with open("ip_scan_lan.txt","w+",encoding="utf8") as fp:
        ip_lan = input("请输入您要检查的网段、子网掩码：")
        ip = IPy.IP(f'{ip_lan}')
        for i in ip:
            try:
                # subprocess.run(args[, stdout, stderr, shell ...])
                # stdout=subprocess.PIPE：表示subprocess在执行命令的时候、将所有的标准输出保存起来不输出在屏幕上
                # check：检查前面的args参数命令执行是否成功、成功不报错、不成功python程序就会报错、python程序就停止
                subprocess.run(f"ping {i} -c 1 -w 1", shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL,
                                        check=True)
                fp.write(f"{i}已被使用\n")
            except:
                fp.write(f"{i}未被使用\n")
